<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleCreate">添加优惠券</el-button>
    </div>
    <div class="card-box">
      <el-table v-loading="couponStore.loading.list" :data="couponStore.list" style="width: 100%">
        <el-table-column prop="name" label="名称" />
        <el-table-column label="供应商" prop="supplier.name" />
        <el-table-column label="金额" prop="amount" />
        <el-table-column label="最低消费" prop="min_purchase" />
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-tag :type="row.is_active ? 'success' : 'danger'">
              {{ row.is_active ? "启用" : "禁用" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="有效期" prop="end_at" />
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-popconfirm title="确定删除?" @confirm="handleDelete(row)">
              <template #reference>
                <el-button link type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-model:page="listQuery.page"
        v-model:limit="listQuery.size"
        :total="couponStore.totalCount"
        @pagination="couponStore.fetchList(listQuery)"
      />
    </div>
    <Form v-model="visible" :type="formType" :currentForm="currentRow" @confirm="handleConfirm" />
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { useCouponStoreHook } from "@/store/modules/coupon/coupon";
import { CouponForm, CouponType } from "@/api/coupon/coupon";
import Form, { type FormType } from "./components/form.vue";
import Pagination from "@/components/Pagination/index.vue";

const couponStore = useCouponStoreHook();

const listQuery = reactive({
  page: 1,
  size: 20,
});

const visible = ref(false);
const formType = ref<FormType>("add");

const currentRow = ref<Partial<CouponForm>>({
  supplier: "",
  is_active: true,
  name: "",
  amount: 0,
  min_purchase: 0,
  start_at: "",
  end_at: "",
  expired_at: "",
  valid_days: 0,
  usage_limit: 0,
  total_limit: 0,
  remaining_quantity: 0,
  remark: "",
});

onMounted(async () => {
  await couponStore.fetchList(listQuery);
});

function handleCreate() {
  formType.value = "add";
  currentRow.value = {
    supplier: "",
    is_active: true,
    name: "",
    amount: 0,
    min_purchase: 0,
    start_at: "",
    end_at: "",
    expired_at: "",
    valid_days: 0,
    usage_limit: 0,
    total_limit: 0,
    remaining_quantity: 0,
    remark: "",
  };
  visible.value = true;
}

function handleEdit(row: CouponType) {
  formType.value = "edit";
  currentRow.value = {
    id: row.id,
    supplier: row.supplier.id,
    is_active: row.is_active,
    name: row.name,
    amount: Number(row.amount),
    min_purchase: Number(row.min_purchase),
    start_at: row.start_at,
    end_at: row.end_at,
    expired_at: row.expired_at,
    valid_days: row.valid_days,
    usage_limit: row.usage_limit,
    total_limit: row.total_limit,
    remaining_quantity: row.remaining_quantity,
    remark: row.remark,
  };
  visible.value = true;
}

async function handleDelete(row: CouponType) {
  await couponStore.fetchDelete(row.id);
  couponStore.fetchList(listQuery);
}

function handleConfirm() {
  couponStore.fetchList(listQuery);
}
</script>
